<?php
require_once ('library/weibo/saetv2.ex.class.php');
class Admin_WeiboController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
         $this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery-1.8.1.min.js');
         $this->view->headScript()->appendFile($this->view->baseUrl().'/js/jquery.masonry.min.js');
        
    }

    public function indexAction()
    {
        $callBackUrl="http://127.0.0.1/txy/admin/weibo/callback";
        $o=Txy_Util_Weibo::getAuthAccess();
        $this->view->codeUrl=$o->getAuthorizeURL($callBackUrl);
    }

    public function callbackAction()
    {
        //session_start();        
        $o=Txy_Util_Weibo::getAuthAccess();
        
        if (isset($_REQUEST['code'])) {
        	$keys = array();
        	$keys['code'] = $_REQUEST['code'];
        	$keys['redirect_uri'] = "http://127.0.0.1/txy/admin/weibo/callback";
        	try {
        		$token = $o->getAccessToken( 'code', $keys ) ;
        	} catch (OAuthException $e) {
        	    echo $e;
        	}
        }
        
        if ($token) {
            var_dump($token);
        	$_SESSION['token'] = $token;
    	    setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
    	    $this->view->flag=1;
        } else {
            echo "授权失败";
        }
    }

    public function wbcheckAction()
    {
        $weiboid=$this->getRequest()->getParam('wbid');
        $isDelete=$this->getRequest()->getParam('radio_'.$weiboid);
        
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
        $db = Zend_Db::factory($config->resources->db);
        if(0==$isDelete){
            $db->query("update txy_temp_weibo set flag=1 where weiboid=".$weiboid);
        }else{
            $db->query("insert into txy_weibo (weiboid,text,multipic,bmiddlepic,orginalpic,createtime,uid) select weiboid,text,multipic,bmiddlepic,orginalpic,createtime,uid from txy_temp_weibo where weiboid=".$weiboid);
            $db->query("update txy_temp_weibo set flag=1 where weiboid=".$weiboid);
            $dbWeibo=new Application_Model_DbTable_Weibo();
            $weiBoArray=$dbWeibo->getWeiboById($weiboid);
            $weiBoLocalConverter=new Txy_Util_WeiboLocalConverter();
            $weiBoLocalConverter->localConverter($weiBoArray);
            
        }
        $this->_helper->redirector('temp-wb-list');
    }

    public function wbUserUpdateAction()
    {
        // action body
        $c = Txy_Util_Weibo::getWeiboAccess();
        $ms  = $c->friends_by_id("3285640003"); // done
        if(is_array($ms['users'])){
            $dbWeiboUser=new Application_Model_DbTable_WeiboUser();
            foreach ($ms['users'] as $user){
                $data['uidstr']=$user['idstr'];
                $data['uname']=$user['name'];
                $data['uimgurl']=$user['profile_image_url'];
                $data['profileurl']=$user['profile_url'];
                $data['description']=$user['description'];
                $data['followers']=$user['followers_count'];
                $data['friends']=$user['friends_count'];
                $data['statuses']=$user['statuses_count'];
                $data['avatar_large']=$user['avatar_large'];
                $data['verified']=$user['verified'];
                $dbWeiboUser->addNew($data);
            }
        }
        //$this->view->ms=$ms['statuses'];
    }

    public function wbUpdateAction()
    {
        // action body
        $c = Txy_Util_Weibo::getWeiboAccess();
        for($i=1;$i<=9;$i++){
            $ms  = $c->home_timeline($i,100,null,null,0,1); 
            if(is_array($ms['statuses'])){
                $dbTempWeibo=new Application_Model_DbTable_TempWeibo();
                foreach ($ms['statuses'] as $weibo){
                    $data=array();
                    $data['weiboid']=$weibo['idstr'];
                    $data['text']=$weibo['text'];
                    if(count($weibo['pic_urls'])>1){
                        $data['multipic']=1;
                        $dbWeiboMultiPics=new Application_Model_DbTable_WeiboMultiPics();
                        foreach ($weibo['pic_urls'] as $picurl){
                            $picData=array();
                            $picData['weiboid']=$weibo['idstr'];
                            $picData['picurl']=$picurl['thumbnail_pic'];
                            $dbWeiboMultiPics->addNewPic($picData);
                        }
                        
                    }
                    
                    if(isset($weibo['bmiddle_pic'])){
                        $data['bmiddlepic']=$weibo['bmiddle_pic'];
                    }
                    if(isset($weibo['original_pic'])){
                        $data['orginalpic']=$weibo['original_pic'];
                    }
                    $data['createtime']=$weibo['created_at'];
                    $data['uid']=$weibo['user']['idstr'];
                    $dbTempWeibo->addNew($data);
                }
            }
        }
        
        
        
        $ms  = $c->home_timeline(); // done
        if(is_array($ms['statuses']))
        $this->view->ms=$ms['statuses'];
    }

    public function tempWbListAction()
    {
        $config=new Zend_Config_Ini(APPLICATION_PATH."/configs/application.ini",'production');
        $db = Zend_Db::factory($config->resources->db);
        $selecter=$db->select()->from(array('w'=>'txy_temp_weibo'),array('w.weiboid','w.multipic','w.text','w.bmiddlepic','u.uimgurl','u.uname'))->joinLeft(array('u'=>'txy_weibo_user'),'w.uid=u.uidstr')->where('w.flag = 0');
        
        $adapter=new Zend_Paginator_Adapter_DbSelect($selecter);
		$paginator = new Zend_Paginator($adapter);
		$paginator->setItemCountPerPage(40);
		$paginator->setCurrentPageNumber($this->_getParam('page'));
		$this->view->paginator = $paginator;
        
    }
    
    public function addSingleWeiboAction(){
        $wbid=$this->getRequest()->getParam('wbid');
        $c = Txy_Util_Weibo::getWeiboAccess();
        //$ms  = $c->show_status('3577554029808734');
        $ms  = $c->show_status('3576821071069623');
        
        var_dump($ms);
    }


}











